﻿Imports System.Text.RegularExpressions

Public Class 商品入库

    Private database As DataAccess

    Private data_Good As DataTable
    Private data_In As DataTable

    Private Sub 商品入库_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        database = New DataAccess
        database.OpenConnection()

        Dim sqlString As String
        sqlString = "select * from tf_f_goods"
        data_Good = database.QueryDatabase(sqlString)

        sqlString = "select * from tf_in_goods"
        data_In = database.QueryDatabase(sqlString)

        '初始化商品类型和商品名称列表
        GoodTypeList_Init()
        GoodNameList_Init()

    End Sub

    Private Sub GoodTypeList_Init()

        ComboBox_Good_Type.Items.Clear()

        '将商品类型数据列在下拉菜单中
        Dim row As DataRow
        For Each row In data_Good.Rows
            'MessageBox.Show("data_Good.Rows1")
            Dim good_type As String
            good_type = row("goods_type")

            ' 去除重复项
            If ComboBox_Good_Type.FindString(good_type) = -1 Then
                ComboBox_Good_Type.Items.Add(good_type)
            End If

        Next row

        If ComboBox_Good_Type.Items.Count > 0 Then
            ComboBox_Good_Type.SelectedIndex = 0
        End If

    End Sub

    Private Sub GoodNameList_Init()

        ComboBox_Good_Name.Items.Clear()

        '从数据库中获取商品名称信息
        Dim good_type As String = ComboBox_Good_Type.Text
        'Dim sqlString As String
        'sqlString = "select goods_name from tf_f_goods where goods_type ='" & good_type & "'"
        'data_Good = database.QueryDatabase(sqlString)

        Dim rows() As DataRow
        rows = data_Good.Select("goods_type ='" & good_type & "'")

        '将商品名称数据列在下拉菜单中
        Dim row As DataRow
        For Each row In rows
            'MessageBox.Show("data_Good.Rows2")
            ComboBox_Good_Name.Items.Add(row("goods_name"))
        Next row

        If ComboBox_Good_Name.Items.Count > 0 Then
            ComboBox_Good_Name.SelectedIndex = 0
        End If

    End Sub

    Private Sub ComboBox_Good_Type_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox_Good_Type.SelectedIndexChanged

        '关闭提示用户信息
        'Button_Add_New_Good.Enabled = False
        'Label_Add_New_Good_Info.ForeColor = Color.Black

        '更新商品名称列表
        GoodNameList_Init()

    End Sub

    Private Function check_input_string(ByVal str As String) As Boolean

        '    是否为空
        '    是否有非法字符

        If Regex.Match(str, "( )+").Success = True Then
            Return False
        End If

        If str = "" Then
            Return False
        End If

        Return True
    End Function

    Private Function get_max_good_id() As Integer

        Dim max_good_id As Integer = 0

        Dim row As DataRow
        For Each row In data_Good.Rows
            If max_good_id <= row.Item("goods_id") Then
                max_good_id = row.Item("goods_id")
            End If
        Next row

        Return max_good_id
    End Function

    Private Function get_good_id(ByVal good_type As String, ByVal good_name As String) As Integer

        Dim rows() = data_Good.Select("goods_type='" & good_type & "' and goods_name='" & good_name & "'")

        If rows.Length = 1 Then
            Dim row As DataRow
            For Each row In rows
                Return row.Item("goods_id")
            Next row
        ElseIf rows.Length > 1 Then
            MessageBox.Show("在数据库中查询商品是出现错误")
            Return -1
        End If

        Return 0
    End Function

    Private Sub Button_In_Good_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button_In_Good.Click

        Dim good_type As String = ComboBox_Good_Type.Text
        Dim good_name As String = ComboBox_Good_Name.Text
        Dim in_number As String = TextBox_In_Number.Text
        Dim good_unit As String = TextBox_Good_Unit.Text
        Dim in_price As String = TextBox_In_Price.Text
        Dim batch_price As String = TextBox_Batch_Price.Text
        Dim retail_price As String = TextBox_Retail_Price.Text
        Dim source_tel As String = TextBox_Source_Tel.Text
        Dim source_addr As String = TextBox_Source_Addr.Text
        Dim remark As String = TextBox_Remark.Text


        '检查输入是否合法
        If Regex.Match(in_number, "^[0-9]+$").Success = False Then
            MessageBox.Show("商品数量有错误")
            Return
        End If
        If check_input_string(good_unit) = False Then
            MessageBox.Show("商品计量单位有错误")
            Return
        End If
        If Regex.Match(in_price, "^[0-9]+$|^[0-9]+(\.)[0-9]{1,2}$").Success = False Then
            MessageBox.Show("商品进货价有错误")
            Return
        End If
        If Regex.Match(batch_price, "^[0-9]+$|^[0-9]+(\.)[0-9]{1,2}$").Success = False Then
            MessageBox.Show("商品批发销售价有错误")
            Return
        End If
        If Regex.Match(retail_price, "^[0-9]+$|^[0-9]+(\.)[0-9]{1,2}$").Success = False Then
            MessageBox.Show("商品零售价有错误")
            Return
        End If
        If Regex.Match(source_tel, "").Success = False Then
            MessageBox.Show("上家电话有错误")
            Return
        End If
        If Regex.Match(source_addr, "").Success = False Then
            MessageBox.Show("货源地有错误")
            Return
        End If

        '检查数据库中是否有此商品
        Dim good_id As Integer = get_good_id(good_type, good_name)
        If good_id = 0 Then
            MessageBox.Show("数据库中没有此商品信息, 请先添加此商品")
            Return
        End If

        '将进货信息存入数据库中

        If in_good_process(good_id, good_type, good_name, in_number, good_unit, in_price, batch_price, _
                           retail_price, source_tel, source_addr, remark) = False Then
            MessageBox.Show("商品入库失败")
        Else
            MessageBox.Show("商品入库成功")
        End If

    End Sub


    Private Function in_good_process(ByVal good_id As String, ByVal good_type As String, _
                                     ByVal good_name As String, ByVal in_number As String, _
                                     ByVal good_unit As String, ByVal in_price As String, _
                                     ByVal batch_price As String, ByVal retail_price As String, _
                                     ByVal source_tel As String, ByVal source_addr As String, _
                                     ByVal remark As String) As Boolean


        '获取入库批次号
        Dim subscribe_id As Integer = get_max_subscribe_id() + 1

        ' 更新入库表
        database.SetSelectCommand("select * from tf_in_goods")

        Dim row As DataRow
        row = data_In.NewRow
        data_In.Rows.Add(row)

        row("subscribe_id") = subscribe_id
        row("goods_id") = good_id
        row("goods_type") = good_type
        row("goods_name") = good_name
        row("in_price") = CSng(in_price)
        row("wholesale_price") = CSng(batch_price)
        row("retail_price") = CSng(retail_price)
        row("num") = CInt(in_number)
        row("unit") = good_unit
        row("tel") = source_tel
        row("goods_source") = source_addr
        row("in_date") = DateTimePicker_In_Date_Time.Value
        row("update_time") = System.DateTime.Now.ToString
        row("remark") = remark

        If database.UpdateDatabase(data_In) = False Then
            MessageBox.Show("更新商品入库数据库时失败")
            Return False
        End If

        '  更新商品实体表
        database.SetSelectCommand("select * from tf_f_goods")

        For Each row In data_Good.Rows
            If good_id = row.Item("goods_id") Then
                row("goods_id") = good_id
                row("goods_type") = good_type
                row("goods_name") = good_name
                row("wholesale_price") = batch_price
                row("retail_price") = retail_price
                row("num") = row("num") + in_number
                row("unit") = good_unit
                'row("update_time") = System.DateTime.Now.ToString
                row("remark") = ""
            End If
        Next row

        If database.UpdateDatabase(data_Good) = False Then
            MessageBox.Show("更新商品入库数据库时失败")

            ' To Do
            ' 删除在入库表中添加的数据

            Return False
        End If

        Return True

    End Function


    Private Function get_max_subscribe_id() As Integer

        Dim max_subscribe_id As Integer = 0

        Dim row As DataRow
        For Each row In data_In.Rows
            If max_subscribe_id <= row.Item("subscribe_id") Then
                max_subscribe_id = row.Item("subscribe_id")
            End If
        Next row

        Return max_subscribe_id
    End Function

End Class